﻿using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Serilog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using TMI85303.Utils;

namespace TMI85303.UI.UserControls
{
    /// <summary>
    /// Status2V1Control.xaml 的交互逻辑
    /// </summary>
    public partial class Status2V1Control : UserControl
    {
        public Status2V1Control()
        {
            InitializeComponent();
        }
    }

    public partial class Status2V1VM : ObservableObject
    {
        [ObservableProperty]
        private string? reg128Data;

        [ObservableProperty]
        private string? reg129Data;

        [ObservableProperty]
        private string? reg130Data;

        [ObservableProperty]
        private string? reg131Data;

        [ObservableProperty]
        private string? reg132Data;

        [ObservableProperty]
        private string? reg133Data;

        [ObservableProperty]
        private string? reg134Data;

        [ObservableProperty]
        private string? reg135Data;

        [ObservableProperty]
        private string? reg136Data;

        [ObservableProperty]
        private string? reg137Data;

        [ObservableProperty]
        private string? reg138Data;

        [ObservableProperty]
        private string? reg139Data;

        [ObservableProperty]
        private string? reg140Data;

        [ObservableProperty]
        private string? reg141Data;

        [ObservableProperty]
        private string? reg142Data;

        [ObservableProperty]
        private string? reg143Data;

        [ObservableProperty]
        private string? reg144Data;

        [ObservableProperty]
        private string? reg145Data;

        [ObservableProperty]
        private double phase_Advance_129;

        [ObservableProperty]
        private double die_Temp_138;

        [ObservableProperty]
        private string? sustainRead129Content = "按照预设时间持续读取所选寄存器";

        [ObservableProperty]
        private bool isChecked128 = false;
        [ObservableProperty]
        private bool isChecked129 = false;
        [ObservableProperty]
        private bool isChecked130 = false;
        [ObservableProperty]
        private bool isChecked131 = false;
        [ObservableProperty]
        private bool isChecked132 = false;
        [ObservableProperty]
        private bool isChecked133 = false;
        [ObservableProperty]
        private bool isChecked134 = false;
        [ObservableProperty]
        private bool isChecked135 = false;
        [ObservableProperty]
        private bool isChecked136 = false;
        [ObservableProperty]
        private bool isChecked137 = false;
        [ObservableProperty]
        private bool isChecked138 = false;
        [ObservableProperty]
        private bool isChecked139 = false;
        [ObservableProperty]
        private bool isChecked140 = false;
        [ObservableProperty]
        private bool isChecked141 = false;
        [ObservableProperty]
        private bool isChecked142 = false;
        [ObservableProperty]
        private bool isChecked143 = false;
        [ObservableProperty]
        private bool isChecked144 = false;
        [ObservableProperty]
        private bool isChecked145 = false;

        [ObservableProperty]
        private bool isReadOnceEnable = true;
        [ObservableProperty]
        private bool selectAllOrNotEnable = true;

        
        private int sustainReadInteralMicsec = 1000;
        public int SustainReadInteralMicsec
        {
            get => sustainReadInteralMicsec;
            set
            {
                if (value <= 0)
                {
                    MessageBox.Show("输入值小于0"); return;
                }
                SetProperty(ref sustainReadInteralMicsec, value);
                sustainReadTime.Interval = TimeSpan.FromMilliseconds(value);
            }
        }

        private readonly DispatcherTimer sustainReadTime = new();

        public Status2V1VM()
        {
            sustainReadTime.Interval = TimeSpan.FromMilliseconds(sustainReadInteralMicsec);
            sustainReadTime.Tick += (s, e) => SustainRead129();
        }

        [RelayCommand]
        private void ContinuousReadMTP(object parameter)
        {
            if (parameter is not bool isChecked) return;

            if (isChecked)
            {
                IsReadOnceEnable = false;
                SelectAllOrNotEnable = false;
                SustainRead129Content = "停止";
                sustainReadTime.Start();
            }
            else
            {
                IsReadOnceEnable = true;
                SelectAllOrNotEnable = true;
                SustainRead129Content = "按照预设时间持续读取所选寄存器";
                sustainReadTime.Stop();
            }
        }

        private void SustainRead129()
        {
            int readData = ReadWrite.ReadReg(129, 2);
            Reg129Data = readData.ToString("X4");
            if (readData > 31)
            {
                readData = 32 - readData;//2023.8.7
            }
            if (((ReadWrite.ReadReg(81, 2) >> 12) & 0xF) == 0)
            {
                readData += 12;
            }
            Phase_Advance_129 = Math.Round(readData * 1.411764705882353,1);
        }

        [RelayCommand]
        private void ReadOnce()
        {
            Log.Information(" 读取状态寄存器\r\n");

            if(IsChecked128) Reg128Data = ReadWrite.ReadReg((ushort)128, 2).ToString("X4");
            int readData;
            if (IsChecked129)
            {
                readData = ReadWrite.ReadReg((ushort)(129), 2);
                if (readData > 31)
                {
                    readData = 32 - readData;//2023.8.7
                }
                if (((ReadWrite.ReadReg(81, 2) >> 12) & 0xF) == 0)
                {
                    readData += 12;
                }
                Phase_Advance_129 = Math.Round(readData * 1.411764705882353, 1);
                Reg129Data = readData.ToString("X4");
            }

            if(IsChecked130) Reg130Data = ReadWrite.ReadReg((ushort)130, 2).ToString("X4");
            if(IsChecked131) Reg131Data = ReadWrite.ReadReg((ushort)131, 2).ToString("X4");
            if(IsChecked132) Reg132Data = ReadWrite.ReadReg((ushort)132, 2).ToString("X4");
            if(IsChecked133) Reg133Data = ReadWrite.ReadReg((ushort)133, 2).ToString("X4");
            if(IsChecked134) Reg134Data = ReadWrite.ReadReg((ushort)134, 2).ToString("X4");
            if(IsChecked135) Reg135Data = ReadWrite.ReadReg((ushort)135, 2).ToString("X4");
            if(IsChecked136) Reg136Data = ReadWrite.ReadReg((ushort)136, 2).ToString("X4");
            if(IsChecked137) Reg137Data = ReadWrite.ReadReg((ushort)137, 2).ToString("X4");
            if(IsChecked138)
            {
                readData = ReadWrite.ReadReg((ushort)(138), 2);
                Die_Temp_138 = 3.0 + (readData - 133.0) / 2;
                Reg138Data = readData.ToString("X4");
            }

            if(IsChecked139) Reg139Data = ReadWrite.ReadReg((ushort)139, 2).ToString("X4");
            if(IsChecked140) Reg140Data = ReadWrite.ReadReg((ushort)140, 2).ToString("X4");
            if(IsChecked141) Reg141Data = ReadWrite.ReadReg((ushort)141, 2).ToString("X4");
            if(IsChecked142) Reg142Data = ReadWrite.ReadReg((ushort)142, 2).ToString("X4");
            if(IsChecked143) Reg143Data = ReadWrite.ReadReg((ushort)143, 2).ToString("X4");
            if(IsChecked144) Reg144Data = ReadWrite.ReadReg((ushort)144, 2).ToString("X4");
            if(IsChecked145) Reg145Data = ReadWrite.ReadReg((ushort)145, 2).ToString("X4");
        }

        [RelayCommand]
        private void SelectAllOrNotAll(object parameter)
        {
            if (parameter is not bool isChecked) return;

            IsChecked128 = isChecked;
            IsChecked129 = isChecked;
            IsChecked130 = isChecked;
            IsChecked131 = isChecked;
            IsChecked132 = isChecked;
            IsChecked133 = isChecked;
            IsChecked134 = isChecked;
            IsChecked135 = isChecked;
            IsChecked136 = isChecked;
            IsChecked137 = isChecked;
            IsChecked138 = isChecked;
            IsChecked139 = isChecked;
            IsChecked140 = isChecked;
            IsChecked141 = isChecked;
            IsChecked142 = isChecked;
            IsChecked143 = isChecked;
            IsChecked144 = isChecked;
            IsChecked145 = isChecked;
        }

    }

}
